﻿var teamId;
var backURL;
var tmTeamPlace={};
var map;
var marker;
$(document).ready(function(){ 
	$('#header').animate({'top':'0px'},400);
	teamId = args.teamId; //取得id的值
	backURL = args.backURL;
	
	//百度地图API功能
	map = new BMap.Map("allmap");    // 创建Map实例
	var point = new BMap.Point(118.778764, 31.981259);
	map.centerAndZoom(point, 15);  // 初始化地图,设置中心点坐标和地图级别
	map.setCurrentCity("南京");          // 设置地图显示的城市 此项是必须设置的
	map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
	var top_left_navigation = new BMap.NavigationControl();  //左上角，添加默认缩放平移控件
	map.addControl(top_left_navigation); 
	
	marker = new BMap.Marker(point);// 创建标注
	map.addOverlay(marker);             // 将标注添加到地图中
	tmTeamPlace.placeLongitude = point.lng;
	tmTeamPlace.placeLatitude = point.lat;

//	map.addEventListener("dragging", showInfo);
	map.addEventListener("moving", showInfo);
	map.addEventListener("movestart", showInfo);
	map.addEventListener("moveend", showInfo);
	
	$("#submitBtn").click(function(){
		var strMessage="";
		//检查必填
		if($("input[id='placeName']").val()==undefined||$.trim($("input[id='placeName']").val())==""){
			strMessage += "场地名称必填!";
			alert(strMessage);
			return false;
		}
		teamPlaceInfo();
		var jsonStr = JSON.stringify(tmTeamPlace);
		var postData={jsonStr:jsonStr};
		$.post(path+"/soccer/tmTeamPlaceAction!saveTeamPlace.action",postData, function(data){
			tmTeamPlace=data.tmTeamPlace;
			//更新当前编辑比赛的地点
			var teamGame=JSON.parse(sessionStorage.getItem("teamGameEdit"));
			if(teamGame!=null&&teamGame!=undefined){
				teamGame.placeId=tmTeamPlace.rowId;
				teamGame.placeName=tmTeamPlace.placeName;
			}
			sessionStorage.setItem("teamGameEdit", JSON.stringify(teamGame));
			goBack();
		},'json');
	});
});
function showInfo(e){
    map.removeOverlay(marker);
    point = map.getCenter();
    marker = new BMap.Marker(point);
    map.addOverlay(marker);             // 将标注添加到地图中
    tmTeamPlace.placeLongitude = point.lng;
    tmTeamPlace.placeLatitude = point.lat;
}
function teamPlaceInfo(){
	if(tmTeamPlace==null){
		tmTeamPlace = {};
	}
	tmTeamPlace.teamId=teamId;
	tmTeamPlace.mdPlaceId=-1;
	tmTeamPlace.placeName=$("input[id='placeName']").val();
	tmTeamPlace.placeDesc=$("input[id='placeDesc']").val();
}
function goBack(){
	var timestamp = (new Date()).valueOf();
	window.location.href=path+backURL+"?teamId="+teamId+"&timestamp="+timestamp;
}
